<?php

class Daftar_distribusi extends CI_Controller {
	
	function Daftar_distribusi()
	{
		parent::__construct();
		$this->load->model('daftar_distribusi_model', '', TRUE);
		$this->load->model('Home_model', '', TRUE);
		$this->load->model('auth_model', '', TRUE);
	}
	
	function index()
	{
		if ($this->session->userdata('login') == FALSE)
		{
			redirect('auth');
		}
		
		// Load default view
		$data['title'] = "Daftar Distribusi";
		$data['main_view'] = 'daftar_distribusi/daftar_distribusi_view';
		
		$id_role = $this->session->userdata('id_role'); 
		$data['hak_akses0'] = $this->Home_model->get_hak_akses($id_role,0);
		$data['hak_akses1'] = $this->Home_model->get_hak_akses($id_role,1);
		$data['hak_akses2'] = $this->Home_model->get_hak_akses($id_role,2);
		
		$data['all_distribusi'] = $this->daftar_distribusi_model->get_all_distribusi();
		
		//var_dump($data['all_distribusi']->result_array());
		// Load view
		$this->load->view('template', $data);
	}
	
	function update_proses(){
	
		$id_distribusi 	= $this->input->post('id_distribusi');
		$revisi 		= $this->input->post('revisi');
		$id_user 		= $this->session->userdata('id_user');
		
		$data = array(
		
			'ID_DISTRIBUSI'	=> $id_distribusi,
			'REVISI'		=> $revisi,
			'ID_USER'		=> $id_user	
		
		);
		
		$this->daftar_distribusi_model->update_proses($data);
		
		if ($revisi == 2){
			
			$dist 	= $this->daftar_distribusi_model->get_stok($id_distribusi);
			var_dump($dist);
			
			$data_tambah 	= array( //distribusi masuk
			
			'ID_USER'			=> $id_user,
			'ID_SAPI' 			=> $dist->ID_SAPI,
			'ID_TITIK_PASOK' 	=> $dist->KE,
			'JUMLAH'			=> $dist->JUMLAH,
			'REVISI'			=> 2,
			'ALASAN_PERUBAHAN'	=> 4,
			'TANGGAL_KEJADIAN'	=> $dist->TANGGAL_KEJADIAN,
			'KOMENTAR'			=> $dist->KOMENTAR,
			
			);
			
			$data_kurang 	= array( //disrtibusi keluar
			
			'ID_USER'			=> $id_user,
			'ID_SAPI' 			=> $dist->ID_SAPI,
			'ID_TITIK_PASOK' 	=> $dist->DARI,
			'JUMLAH'			=> ($dist->JUMLAH)*-1,
			'REVISI'			=> 2,
			'ALASAN_PERUBAHAN'	=> 5,
			'TANGGAL_KEJADIAN'	=> $dist->TANGGAL_KEJADIAN,
			'KOMENTAR'			=> $dist->KOMENTAR,
			
			);
			
			$this->daftar_distribusi_model->insert_to_stok($data_tambah);
			$this->daftar_distribusi_model->insert_to_stok($data_kurang);
			
		}
		
	}
	
}